﻿<!--#include file="../lib/base.asp"-->
<!--#include file="../theme.asp"-->
<%
dim act:act=lcase(sdcms.fget("act",0))
dim formvalue:formvalue=sdcms.fpost(act,0)

function errcode(byval t0)
	errcode="{""error"":"""&t0&"""}"
end function

function succcode()
	succcode="{""ok"":""""}"
end function

sub checkusername()
	if sdcms.strlen(formvalue)=0 then
		sdcms.echo errcode("&#29992;&#25143;&#21517;&#19981;&#33021;&#20026;&#31354;")
		exit sub
	end if
	if not(sdcms.checkstr(formvalue,"username")) then
		sdcms.echo errcode("&#30001;&#53;&#45;&#50;&#48;&#20301;&#23383;&#27597;&#12289;&#25968;&#23383;&#12289;&#19979;&#21010;&#32447;&#32452;&#25104;")
		exit sub
	end if
	
	
	dim baduser:baduser=sdcms.getsys("baduser")
	if sdcms.strlen(baduser)>0 then
		baduser="|"&baduser&"|"
		if instr(baduser,"|"&formvalue&"|")>0 then
			sdcms.echo errcode("&#31995;&#32479;&#31105;&#27490;&#27880;&#20876;&#27492;&#29992;&#25143;&#21517;&#65292;&#35831;&#25442;&#20010;&#35797;&#35797;")
			exit sub
		end if
	end if
	if sdcms.db.dbcount("sd_user","username='"&formvalue&"'")>0 then
		sdcms.echo errcode("&#29992;&#25143;&#21517;&#24050;&#23384;&#22312;&#65292;&#35831;&#25442;&#20010;&#35797;&#35797;")
		exit sub
	end if
	sdcms.echo succcode
end sub




'判断输入的是email还是tel
sub checkureg()

	dim formvalue:formvalue=sdcms.enhtml(sdcms.fget("fvalue",0))
	if sdcms.strlen(formvalue)=0 then
	sdcms.ajaxjson "请输入邮箱或手机号",0
	 exit sub
	end if
	if sdcms.checkstr(formvalue,"email") or sdcms.checkstr(formvalue,"mobile") then
    if sdcms.checkstr(formvalue,"email") then
	if sdcms.db.dbcount("sd_user","email='"&formvalue&"'")>0 then
		sdcms.ajaxjson "电子邮箱已存在，请换个试试",0
		exit sub
	end if
	sdcms.ajaxjson "1邮箱验证通过",1
	else
		if sdcms.db.dbcount("sd_user","tel='"&formvalue&"'")>0 then
		sdcms.ajaxjson "手机号已存在，请直接登录或找回密码试试",0
		exit sub
	end if
	sdcms.ajaxjson "2手机验证通过",1
	end if
	else
	sdcms.ajaxjson "请输入正确的邮箱号或手机号",0
	end if
end sub


Function gen_key(digits)	
dim output,num
	'Create and define array
	dim char_array(50)
	char_array(0) = "0"
	char_array(1) = "1"
	char_array(2) = "2"
	char_array(3) = "3"
	char_array(4) = "4"
	char_array(5) = "5"
	char_array(6) = "6"
	char_array(7) = "7"
	char_array(8) = "8"
	char_array(9) = "9"
	
	'Initiate randomize method for default seeding
	randomize
	
	'Loop through and create the output based on the the variable passed to
	'the function for the length of the key.
	do while len(output) < digits
	num = char_array(Int((9 - 0 + 1) * Rnd + 0))
	output = output + num
	loop
	
	'Set return
	gen_key = output
End Function

'发送验证码
sub sendsms()

	dim formvalue:formvalue=sdcms.enhtml(sdcms.fget("fvalue",0))
	if sdcms.strlen(formvalue)=0 then
	sdcms.ajaxjson "请填写正确的手机号",0
	 exit sub
	end if
	if sdcms.checkstr(formvalue,"mobile") then
	dim code:code=gen_key(4)
	sdcms.setsession "code",code
    dim out:out=sdcms.sendsms ("您的验证码是："&code&"。请不要把验证码泄露给其他人。",formvalue)
	sdcms.ajaxjson out,1
	end if
	

end sub

'发送找回密码验证码
sub sendgetpasssms()

	dim formvalue:formvalue=sdcms.enhtml(sdcms.fget("fvalue",0))
	if sdcms.strlen(formvalue)=0 then
	sdcms.ajaxjson "请填写正确的手机号",0
	 exit sub
	end if
	
	if sdcms.checkstr(formvalue,"mobile") then
	if sdcms.db.dbcount("sd_user","username='"&formvalue&"'")>0 then
	dim code:code=gen_key(4)
	sdcms.setsession "getpasscode",code
    dim out:out=sdcms.sendsms ("您的验证码是："&code&"。请不要把验证码泄露给其他人。",formvalue)
	sdcms.ajaxjson out,1
	else
	sdcms.ajaxjson "该手机号未注册！",0
	end if
	else
	sdcms.ajaxjson "请填写正确的手机号！",0
	end if
	

end sub

sub checkemail()
	if sdcms.strlen(formvalue)=0 then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#19981;&#33021;&#20026;&#31354;")
		exit sub
	end if
	if not(sdcms.checkstr(formvalue,"email")) then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#26684;&#24335;&#19981;&#27491;&#30830;")
		exit sub
	end if
	if sdcms.db.dbcount("sd_user","email='"&formvalue&"'")>0 then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#24050;&#23384;&#22312;&#65292;&#35831;&#25442;&#20010;&#35797;&#35797;")
		exit sub
	end if
	sdcms.echo succcode
end sub

sub checkemailpass()
	if sdcms.strlen(formvalue)=0 then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#19981;&#33021;&#20026;&#31354;")
		exit sub
	end if
	if not(sdcms.checkstr(formvalue,"email")) then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#26684;&#24335;&#19981;&#27491;&#30830;")
		exit sub
	end if
	if sdcms.db.dbcount("sd_user","email='"&formvalue&"'")<=0 then
		sdcms.echo errcode("&#24744;&#36755;&#20837;&#30340;&#30005;&#23376;&#37038;&#31665;&#19981;&#23384;&#22312;")
		exit sub
	end if
	sdcms.echo succcode
end sub

sub checkemailedit()
	if sdcms.strlen(formvalue)=0 then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#19981;&#33021;&#20026;&#31354;")
		exit sub
	end if
	if not(sdcms.checkstr(formvalue,"email")) then
		sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#26684;&#24335;&#19981;&#27491;&#30830;")
		exit sub
	end if
	dim userdata:userdata=sdcms.userinfo
	if formvalue<>userdata(3) then
		if sdcms.db.dbcount("sd_user","email='"&formvalue&"'")>0 then
			sdcms.echo errcode("&#30005;&#23376;&#37038;&#31665;&#24050;&#23384;&#22312;&#65292;&#35831;&#25442;&#20010;&#35797;&#35797;")
			exit sub
		end if
	end if
	sdcms.echo succcode
end sub

sub checkimgcode()
	if sdcms.strlen(formvalue)=0 then
		sdcms.echo errcode("&#39564;&#35777;&#30721;&#19981;&#33021;&#20026;&#31354;")
		exit sub
	end if
	if sdcms.loadsession("imgcode")<>formvalue then
		sdcms.echo errcode("&#39564;&#35777;&#30721;&#38169;&#35823;")
		exit sub
	end if
	sdcms.echo succcode
end sub

sub checkoldpass()
	dim userdata:userdata=sdcms.userinfo
	if md5(formvalue)<>userdata(2) then
		sdcms.echo errcode("&#21407;&#23494;&#30721;&#19981;&#27491;&#30830;")
		exit sub
	end if
	sdcms.echo succcode
end sub

sub delfavorite()
if not(sdcms.is_login) then
sdcms.echo ("0&#24744;&#38656;&#35201;&#30331;&#24405;&#21518;&#25165;&#33021;&#25910;&#34255;")
exit sub
end if
dim t0:t0=sdcms.getint(sdcms.fpost("id",1),0)
	if t0=0 then
		sdcms.echo ("1&#21442;&#25968;&#38169;&#35823;")
		exit sub
	end if
	if sdcms.db.dbcount("sd_content","id="&t0&" and islock=1")=0 then
		sdcms.echo ("1&#21442;&#25968;&#38169;&#35823;")
		exit sub
	end if
	dim userdata,userid
	userdata=sdcms.userinfo
	userid=userdata(0)
	
	if sdcms.db.dbcount("sd_user_favorite","contentid="&t0&" and userid="&userid&"")>0 then
		sdcms.db.dbdel "sd_user_favorite","contentid="&t0&" and userid="&userid&""
		sdcms.echo ("1&#21462;&#28040;&#25910;&#34255;")
     end if		
	
end sub

sub checkfavorite()
	if not(sdcms.is_login) then
		sdcms.echo ("0&#24744;&#38656;&#35201;&#30331;&#24405;&#21518;&#25165;&#33021;&#25910;&#34255;")
		exit sub
	end if
	dim t0:t0=sdcms.getint(sdcms.fpost("id",1),0)
	if t0=0 then
		sdcms.echo ("1&#21442;&#25968;&#38169;&#35823;")
		exit sub
	end if
	if sdcms.db.dbcount("sd_content","id="&t0&" and islock=1")=0 then
		sdcms.echo ("1&#21442;&#25968;&#38169;&#35823;")
		exit sub
	end if
	
	dim userdata,userid
	userdata=sdcms.userinfo
	userid=userdata(0)
	if sdcms.db.dbcount("sd_user_favorite","contentid="&t0&" and userid="&userid&"")>0 then
		sdcms.echo ("1&#24050;&#25910;&#34255;")
		exit sub
	else
		sdcms.db.insert "sd_user_favorite",array(array("userid",userid,0,0),array("contentid",t0,0,0),array("createdate",sqltime,0,0))
		sdcms.echo ("1&#25910;&#34255;&#25104;&#21151;")
	end if
end sub

select case act
	case "username":checkusername
	case "ureg":checkureg
	case "sendsms":sendsms
	case "sendgetpasssms":sendgetpasssms
	case "email":checkemail
	case "emailpass":checkemailpass
	case "emailedit":checkemailedit
	case "imgcode":checkimgcode
	case "oldpass":checkoldpass
	case "favorite":checkfavorite
	case "delfavorite":delfavorite
	case else
		sdcms.echo "{""error"":"""&act&"""}"
end select

sdcms.db.dbclose
%>